package example.fxa.com.demo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    String tag = "MainActivity";

    /**
     * 结果显示
     */
    TextView text_history;

    /**
     * 结果显示
     */
    TextView text_result;

    /**
     * 历史操作
     */
    StringBuilder history = new StringBuilder();

    /**
     * 一次计算结果区域
     */
    StringBuilder resultArea = new StringBuilder();

    /**
     * 输入区
     */
    StringBuilder scanArea = new StringBuilder();

    /**
     * 操作符
     */
    private volatile String op;


    List<Button> numberBtns = new ArrayList<>();

    List<Button> opBtns = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.e(tag, "onCreate");
        text_result = (TextView) findViewById(R.id.text_result);
        text_history = (TextView) findViewById(R.id.text_history);
        text_history.setMovementMethod(new ScrollingMovementMethod());
        findViewById(R.id.btn_per).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        findViewById(R.id.btn_clear).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                text_result.setText("");
                resultArea = null;
                scanArea = null;
            }
        });

        numberBtns.add((Button) findViewById(R.id.btn_point));
        numberBtns.add((Button) findViewById(R.id.btn_0));
        numberBtns.add((Button) findViewById(R.id.btn_1));
        numberBtns.add((Button) findViewById(R.id.btn_2));
        numberBtns.add((Button) findViewById(R.id.btn_3));
        numberBtns.add((Button) findViewById(R.id.btn_4));
        numberBtns.add((Button) findViewById(R.id.btn_5));
        numberBtns.add((Button) findViewById(R.id.btn_6));
        numberBtns.add((Button) findViewById(R.id.btn_7));
        numberBtns.add((Button) findViewById(R.id.btn_8));
        numberBtns.add((Button) findViewById(R.id.btn_9));

        opBtns.add((Button) findViewById(R.id.btn_plus));
        opBtns.add((Button) findViewById(R.id.btn_minus));
        opBtns.add((Button) findViewById(R.id.btn_multiplication));
        opBtns.add((Button) findViewById(R.id.btn_divide));

        for (Button button : numberBtns) {
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onValueNumberClick((Button) v);
                }
            });
        }


        for (Button button : opBtns) {
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    switch (((Button) v).getText().toString()) {
                        case "+":
                            op = "+";
                            update();
                            break;
                        case "-":
                            op = "-";
                            update();
                            break;
                        case "*":
                            op = "*";
                            update();
                            break;
                        case "/":
                            op = "/";
                            update();
                            break;
                    }
                }
            });
        }


        findViewById(R.id.btn_equals).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                float lv, rv, result = 0;
                switch (op) {
                    case "+":
                        lv = Float.parseFloat(resultArea.toString());
                        rv = Float.parseFloat(scanArea.toString());
                        result = plus(lv, rv);
                        afterCalculate(result);
                        break;
                    case "-":
                        lv = Float.parseFloat(resultArea.toString());
                        rv = Float.parseFloat(scanArea.toString());
                        result = minus(lv, rv);
                        afterCalculate(result);
                        break;
                    case "*":
                        lv = Float.parseFloat(resultArea.toString());
                        rv = Float.parseFloat(scanArea.toString());
                        result = multiplication(lv, rv);
                        afterCalculate(result);
                        break;
                    case "/":
                        lv = Float.parseFloat(resultArea.toString());
                        rv = Float.parseFloat(scanArea.toString());
                        result = divide(lv, rv);
                        afterCalculate(result);
                        break;
                }
            }
        });

    }

    void update() {
        resultArea.append(scanArea);
        scanArea.setLength(0);
        history.append(resultArea).append("\n").append(op).append("\n");
        text_history.setText(history.toString());
    }

    void afterCalculate(float result) {
        resultArea.setLength(0);
        resultArea.append(result);
        history.append(scanArea).append("\n").append("=").append("\n").append(resultArea);
        scanArea.setLength(0);
        text_history.setText(history.toString());
        text_result.setText("");
    }

    void onValueNumberClick(Button btn) {
        scanArea.append(String.valueOf(btn.getText()));
        text_result.setText(scanArea.toString());
    }



    public native String stringFromJNI(String res);

    public native float plus(float a, float b);

    public native float minus(float a, float b);

    public native float divide(float a, float b);

    public native float multiplication(float a, float b);

    static {
        System.loadLibrary("native-lib");
    }

}
